﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GA_Scheduler
{
    public class SpacePenalty : IPenalty
    {

        public int Score
        {
            get;
            set;
        }

        public string Name
        {
            get { return "SpacePenalty"; }
            set { ; }
        }

        public int CalculatedScore
        {
            get;
            private set;
        }

        public bool Execute(Schedule sch)
        {
            CalculatedScore = 0;
            foreach (var gr in Configuration.Instance.Groups)
            {
                for (int i = 0; i < GA_Scheduler.Properties.Settings.Default.CountDays; i++)
                {
                    var dailyCourses = sch.Genes.Where(gen => gen.DayOfWeek == i &&
                                       gen.Class.GroupClasses
                                       .FirstOrDefault(g => g.Groupd_ID == gr.Group_ID) != null)
                                       .OrderBy(gg => gg.Pair)
                                       .ToList();

                    if (dailyCourses.Count == 0)
                        continue;

                    int lastEndPair = dailyCourses[0].Pair;
                    for (int j = 0; j < dailyCourses.Count; j++)
                    {
                        if (dailyCourses[j].Pair != lastEndPair)
                        {
                            CalculatedScore += Score;
                        }
                        lastEndPair = dailyCourses[j].Pair + dailyCourses[j].Class.Duration;
                    }
                }
            }

            return CalculatedScore != 0;
        }
    }
}
